package com.wyp168.leetcode;

/**
 * @ClassName MaskPIISolution
 * @Title Leetcode.831 [middle] 隐藏个人信息  https://leetcode.cn/problems/masking-personal-information/
 * @Description TODO
 * @Author wyp168
 * @Date 2023/4/1 21:33
 */
public class MaskPIISolution {

    public static void main(String[] args) {
        MaskPIISolution solution = new MaskPIISolution();
        System.out.println(solution.maskPII("86-(10)12345678"));

//        System.out.println(solution.numCount("123123123"));
    }


    public String maskPII(String s) {
        if (s.contains("@")) {
            //包含，就说明是邮箱
            return maskEmail(s);
        } else {
            // 说明是电话号码，则对电话号码进行加密操作
            return maskPhoneNum(s);
        }
    }

    public String maskEmail(String s) {
        s = s.toLowerCase();
        String[] split = s.split("@");
        String result = "";
        result += split[0].substring(0, 1) + "*****" + split[0].substring(split[0].length() - 1) + "@" + split[1];
        return result;
    }

    public String maskPhoneNum(String s) {
        switch (numCount(s)) {
            case 10:
                return "***-***-" + lastForChars(s);
            case 11:
                return "+*-***-***-" + lastForChars(s);
            case 12:
                return "+**-***-***-" + lastForChars(s);
            case 13:
                return "+***-***-***-" + lastForChars(s);
            default:
                return "***-***-****";
        }
    }

    public int numCount(String s) {
        int count = 0;
        for(int i = 0; i < s.length(); i++) {
            if (s.charAt(i) <= '9' && s.charAt(i) >= '0') {
                count ++;
            }
        }
        return count;
    }

    public String lastForChars(String s) {
        StringBuilder result = new StringBuilder();
        int count = 0;
        for(int i = s.length() - 1; i >= 0; i --) {
            if (count == 4) {
                break;
            }
            if (s.charAt(i) <= '9' && s.charAt(i) >= '0') {
                result.insert(0, s.charAt(i));
                count ++;
            }
        }
        return result.toString();
    }
}
